#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/10/26 14:19
# @Author  : HYCX-AI-LAB Liang jinhao

"""
检测是否存在焊珠
"""

from utils.MQ.message import send_message
import cv2
import json
from utils.detection_3d.solder_splash import *


class DetectTinBeads():
    def __init__(self, MQ_send, connection_send, task_id, task_type, npy, png):
        # MQ
        self.MQ_send = MQ_send
        self.connection_send = connection_send

        self.task_id = task_id
        self.task_type = task_type

        self.npy = npy
        self.png = png
        self.mask43 = np.load("utils/detection_3d/mask_4-3.npy")
        self.mask44 = np.load("utils/detection_3d//mask_4-4.npy")
        self.mask45 = np.load("utils/detection_3d//mask_4-5.npy")

        global_cfg = "./global_cfg.json"
        with open(global_cfg, 'r') as f:
            config = json.load(f)
            self.AREA_43 = config['std-43-area']
            self.AREA_44 = config['std-44-area']
            self.AREA_45 = config['std-45-area']
            self.area_threshold = config['fly-tin-threshold']

    def detect(self):
        # start = time.time()
        # 清理边缘
        data = clean_height(self.npy)
        xyz_points = parse_npy_(data)
        x_points, y_points, z_points = xyz_points

        # 读取标准件区域
        std_4_3 = np.load(STD_4_3)
        std_4_4 = np.load(STD_4_4)
        std_4_5 = np.load(STD_4_5)

        # 飞锡差值二值化
        # r43, y43, x43 = visualization_tin_diff(x_points, y_points, z_points, std_4_3, data, (580, 1100))
        # r44, y44, x44 = visualization_tin_diff(x_points, y_points, z_points, std_4_4, data, (195, 2050))
        # r45, y45, x45 = visualization_tin_diff(x_points, y_points, z_points, std_4_5, data, (10, 2030))

        r43, y43, x43 = calculator_tin_diff_with_mask(x_points, y_points, z_points, std_4_3, data,
                                                      (self.AREA_43[2], self.AREA_43[0]), self.mask43, area="43")
        # show_3d(std_4_3, "std_4-3")
        r44, y44, x44 = calculator_tin_diff_with_mask(x_points, y_points, z_points, std_4_4, data,
                                                      (self.AREA_44[2], self.AREA_44[0]), self.mask44, area="44")
        # show_3d(std_4_4, "std_4-4")
        r45, y45, x45 = calculator_tin_diff_with_mask(x_points, y_points, z_points, std_4_5, data,
                                                      (self.AREA_45[2], self.AREA_45[0]), self.mask45, area="45")
        status43 = 0
        status44 = 0
        status45 = 0
        if r43 >= self.area_threshold[0]:
            status43 = 1
        if r44 >= self.area_threshold[1]:
            status44 = 1
        if r45 >= self.area_threshold[2]:
            status45 = 1
        return (status43, status44, status45)

    def send_res(self):
        temp_res = self.detect()
        res = {
            "task_id": self.task_id,
            "task_type": "tin_beads_detect",
            "npy": self.npy,
            "4-3": temp_res[0],
            "4-4": temp_res[1],
            "4-5": temp_res[2]
        }
        send_message(self.MQ_send, self.connection_send, json.dumps(res, ensure_ascii=False))

    def test_detect(self):
        # start = time.time()
        # 清理边缘
        data = clean_height(self.npy)
        xyz_points = parse_npy_(data)
        x_points, y_points, z_points = xyz_points

        # 读取标准件区域
        std_4_3 = np.load(STD_4_3)
        std_4_4 = np.load(STD_4_4)
        std_4_5 = np.load(STD_4_5)

        # 飞锡差值二值化
        # r43, y43, x43 = visualization_tin_diff(x_points, y_points, z_points, std_4_3, data, (580, 1100))
        # r44, y44, x44 = visualization_tin_diff(x_points, y_points, z_points, std_4_4, data, (195, 2050))
        # r45, y45, x45 = visualization_tin_diff(x_points, y_points, z_points, std_4_5, data, (10, 2030))

        r43, y43, x43 = calculator_tin_diff_with_mask(x_points, y_points, z_points, std_4_3, data,
                                                      (self.AREA_43[2], self.AREA_43[0]), self.mask43, area="43")
        # show_3d(std_4_3, "std_4-3")
        r44, y44, x44 = calculator_tin_diff_with_mask(x_points, y_points, z_points, std_4_4, data,
                                                      (self.AREA_44[2], self.AREA_44[0]), self.mask44, area="44")
        # show_3d(std_4_4, "std_4-4")
        r45, y45, x45 = calculator_tin_diff_with_mask(x_points, y_points, z_points, std_4_5, data,
                                                      (self.AREA_45[2], self.AREA_45[0]), self.mask45, area="45")
        status43 = 0
        status44 = 0
        status45 = 0
        if r43 >= self.area_threshold[0]:
            status43 = 1
        if r44 >= self.area_threshold[1]:
            status44 = 1
        if r45 >= self.area_threshold[2]:
            status45 = 1
        return [(r43, r44, r45), (status43, status44, status45)]
